﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace SmartMeterParamSetting.UI
{
    /// <summary>
    /// UI_Config.xaml 的交互逻辑
    /// </summary>
    public partial class UI_Config : Window, SmartMeterParamFrame.IApplication
    {
        private bool m_IsSaveOK = false;

        #region IApplication 成员

        public ContentControl ControlPanel
        {
            get { return TabDevice; }
        }

        public bool IsSaveOK
        {
            set { m_IsSaveOK = value; }
        }

        public event EventHandler Save;

        #endregion

        #region IServiceProvider 成员

        public object GetService(Type serviceType)
        {
            return new System.ComponentModel.Design.ServiceContainer().GetService(serviceType);
        }

        #endregion

        public UI_Config()
        {
            InitializeComponent();

            Cmb_IdentityType.ItemsSource = Enum.GetNames(typeof(SmartMeterParamFrame.EnumIdentityType));

            Cmb_IdentityType.SelectedItem = SmartMeterParamFrame.EnumIdentityType.自动认证.ToString();

            this.Loaded += new RoutedEventHandler(UI_Config_Loaded);

            this.Closing += new System.ComponentModel.CancelEventHandler(UI_Config_Closing);
        }

        private void UI_Config_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!m_IsSaveOK) e.Cancel = true;
        }

        /// <summary>
        /// 设备驱动是否加载
        /// </summary>
        public bool IsDeviceLoaded
        {
            set
            {
                if (!value)
                {
                    if (tabControl.Items.Count == 1) return;
                    try
                    {
                        tabControl.Items.Remove(TabDevice);
                    }
                    catch { }
                }

            }
        }

        private void UI_Config_Loaded(object sender, RoutedEventArgs e)
        {
            Grid_System.DataContext = Mod_Main.systemInfo;
        }

        private void Btn_OK_Click(object sender, RoutedEventArgs e)
        {
            Mod_Main.systemInfo.Save();

            if (Save != null)
            {
                Save(sender, e);
            }
            else
            {
                m_IsSaveOK = true;
            }

            DateTime Dt = DateTime.Now;

            while (!m_IsSaveOK)
            {
                if (Math.Abs(TimeSub(DateTime.Now, Dt)) > 500) break;      //1秒超时

                System.Threading.Thread.Sleep(20);
            }
            this.DialogResult = true;
        }

        private void Btn_Cancel_Click(object sender, RoutedEventArgs e)
        {
            m_IsSaveOK = true;
            this.DialogResult = false;
        }



        /// <summary>
        /// 事件计算函数
        /// </summary>
        /// <param name="Time1"></param>
        /// <param name="Time2"></param>
        /// <returns></returns>
        private long TimeSub(DateTime Time1, DateTime Time2)
        {
            TimeSpan tsSub = Time1.Subtract(Time2);
            return tsSub.Hours * 60 * 60 * 1000 + tsSub.Minutes * 60 * 1000 + tsSub.Seconds * 1000 + tsSub.Milliseconds;
        }
    }
}
